# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=njs
# Keep it in sync with njs in main/nginx.
pkgver=0.8.4
pkgrel=0
pkgdesc="njs scripting language CLI utility"
url="https://nginx.org/en/docs/njs/"
arch="all"
license="BSD-2-Clause"
# Note: njs is compatible with pcre2, but better to use the same PCRE library
# as for njs module in nginx.
makedepends="libedit-dev libxml2-dev openssl-dev>3 pcre-dev"
checkdepends="expect"
subpackages="$pkgname-debug $pkgname-libs-static"
source="$pkgname-$pkgver.tar.gz::https://hg.nginx.org/njs/archive/$pkgver.tar.gz
	mktemp-busybox-compat.patch
	no-werror.patch
	"

case "$CARCH" in
	x86) options="!check";;  # one test fails
esac

# secfixes:
#   0.7.3-r0:
#     - CVE-2021-46462
#     - CVE-2021-46463
#     - CVE-2022-25139
#   0.7.1-r0:
#     - CVE-2021-46461

build() {
	./configure \
		--build-dir='build-debug' \
		--debug=YES
	make njs

	./configure --cc-opt='-Os' --ld-opt='-Os'
	make njs
}

check() {
	make test
}

package() {
	install -m 755 -D build/njs "$pkgdir"/usr/bin/njs

	install -m 644 -D build/libnjs.a "$pkgdir"/usr/lib/libnjs.a
	install -m 644 -D build-debug/libnjs.a "$pkgdir"/usr/lib/libnjs-debug.a
}

debug() {
	pkgdesc="njs built with additional runtime checks and debug symbols"
	options="!strip"

	install -m 755 -D "$builddir"/build-debug/njs "$subpkgdir"/usr/bin/njs-debug
}

sha512sums="
f2e897d82c1e1ecccd310032f0166ec907e61f3d0756c2231d1f042b25e356effcce5d59bbcfdc3f8d51c30639675987a44f363370dfc7215342f1a498de1169  njs-0.8.4.tar.gz
4db527d663dbe9e8b503c3cbaa4eae34b45990a5359b3bb98ce970c705faefcac98de49439f2557756a2be8e2e06acc67f98942de01674c498832d80c3cb90c3  mktemp-busybox-compat.patch
b8ed822bf0d2ac99682cdecde5ce0136ac46cc9e6077be1369c631ac4caf22f22750f39fced061233f44e3e290e8ca6135da5d121bd53551cbd460419bc90662  no-werror.patch
"
